import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import moment from "moment";

const HOUR_MINUTE_SECOND = 'YYYY-MM-DD HH:mm:ss'
const DAY_MINUTE_SECOND = 'YYYYMMDD_HHmmss'
const DAY_FORMAT = 'YYYY-MM-DD'
// 初始化时间
dayjs.extend(utc)

/** 'YYYY-MM-DD HH:mm:ss'  */

export function timeFormat(utc, format = HOUR_MINUTE_SECOND) {
  return dayjs.utc(utc).utcOffset(8).format(format)
}

/** 'YYYYMMDD_HHmmss' */
export function dayTimeFormat(utc, format = DAY_MINUTE_SECOND) {
  return dayjs.utc(utc).utcOffset(8).format(format)
}
/** 'YYYY-MM-DD' */
export function dayFormat(utc, format = DAY_FORMAT) {
  return dayjs.utc(utc).utcOffset(8).format(format)
}

/** 最后更新时间到浏览器缓存中 */
export function setLastTime(lastTime){
  // beginTime格式: 2024-10-1
  localStorage.setItem('timeFormat_lastTime', lastTime);
}

/** 最后更新时间到浏览器缓存中 */
export function getLastTime(){
   return localStorage.getItem('timeFormat_lastTime');
}

export function addDay(day){
  // 最小时间是今天
  const  date = new Date()
  // 根据开始时间进行判定
  const beginTimeStr = getLastTime();
  if(beginTimeStr && beginTimeStr != ''){
    const beginLong = moment(beginTimeStr);
    // 续费, 当时间大于当且时间, 
    if(beginLong && beginLong>date.getTime()){
      date.setTime(beginLong)
    }
  }

  date.setTime(date.getTime() + 3600 * 1000 * 24 * day)

  return date
}
 
// 左边菜单选择
export function shortcutDays(){
  const shortcuts = [
    {
      text: '+1日',
      value: () => {
        return addDay(1)
      },
    },
    {
      text: '+2周',
      value: () => {
        return addDay(14)
      },
    },
    {
      text: '+1月',
      value: () => {
        return addDay(31)
      },
    },
    {
      text: '+3月',
      value: () => {
        return addDay(31*3)
      },
    },
    {
      text: '+半年',
      value: () => {
        return addDay(31*6)
      },
    },
    {
      text: '+1年',
      value: () => {
        return addDay(31*12)
      },
    },
  ]
  return shortcuts
}

export function yesterday() {
  const today = new Date();
  const yesterday = new Date(today);
  yesterday.setDate(yesterday.getDate() - 1);
  return yesterday;
}
 

/** yyyy-MM-dd */
export function formatDate(date){
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`;
}

/** yyyyMMdd */
export function formatDay(date){
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  return `${year}${month}${day}`;
}

/**  禁用今天之前, (今天也不可选) */
export function disabledBeforeToday(time) {
  return time.getTime() > (yesterday())
}

/**  禁用今天之前, (今天可选) */
export function disabledEndToday(time) {
  return time.getTime() > (new Date())
}
/** 昨天: yyyyMMdd */
export function yesterdayFormatDay() {
  return formatDay(yesterday())
}

/** 今日: yyyyMMdd */
export function todayFormatDay() {
  return formatDay(new Date())
}


export function todayAddDay(day){
  // 最小时间是今天
  const  date = new Date()
  date.setTime(date.getTime() + 3600 * 1000 * 24 * day)
  return date
}
// yyyy-mm-dd
export function timeFormatBegin(day) {
  const addDate = todayAddDay(day)
  return formatDate(addDate)
}
// yyyymmdd
export function dayFormatDay(day) {
  const addDate = todayAddDay(day)
  return formatDay(addDate)
}

export function todayAddDayArray(day) {
  // 今日
  let end = todayAddDay(0)
  let start = todayAddDay(0)
  // 过去N天
  if(day<0){
    end = todayAddDay(-1)
    start = todayAddDay(day)
  }

  return [dayFormat(start), dayFormat(end)]
}
// yyyymmdd
export function todayAddDayArrayLong(day) {
  // 今日
  let end = todayAddDay(0)
  let start = todayAddDay(0)
  // 过去N天
  if(day<0){
    end = todayAddDay(-1)
    start = todayAddDay(day)
  }
  // yyyymmdd
  return [formatDay(start), formatDay(end)]
}



/**
 *  当且时间添加月
 * @param {*} mth 添加月份: 上月: -1, 本月: 0 ,下月: 1
 * @returns 添加月时间
 */
function monthAdd(mth){
  
  let year = parseInt(mth /12)
  let addMonth = parseInt(mth %12)
  let time =  dayjs()

  // 添加年, 然后添加月
  if(year!=0){
    return  time.add(year, 'year').add(addMonth, 'month')
  }
  return time.add(mth, 'month')
}


export function monthFirstDay(mth){

  let monthTime =  monthAdd(mth)
  let startDayTime =  monthTime.startOf('month')
  return  startDayTime.format(DAY_FORMAT)
}

export function monthLastDay(mth){
   let monthTime = monthAdd(mth)
   let endDayTime =  monthTime.endOf('month')
   // 大于今天, 
   if(dayjs().isBefore(endDayTime)){
      return  formatDate(new Date())
   }
  return  endDayTime.format(DAY_FORMAT)
}

export function  nowAddMonth(mth){
  let firstDayStr =  monthFirstDay(mth)
  let lastDayStr =  monthLastDay(mth)
  return [firstDayStr,lastDayStr]
}

export function  lastWeekFirst(wk){
  let startDayTime =  dayjs().add(wk, 'week').startOf('week')
  return  startDayTime.format(DAY_FORMAT)
}

export function  lastWeekLast(wk){
  let lastDayTime =  dayjs().add(wk, 'week').endOf('week')
   // 大于今天, 
  if(dayjs().isBefore(lastDayTime)){
      return  formatDate(new Date())
  }
  return  lastDayTime.format(DAY_FORMAT)
}



/**
 * 
 * @param {*} mthStr yyyy-MM
 * @returns yyyy-MM-dd
 */
export function monthFirstDayStr(mthStr){
  let monthTime = dayjs(mthStr, "YYYY-MM")
  let firstDayTime =  monthTime.startOf('month')
  return  firstDayTime.format(DAY_FORMAT)
}

/**
 * @param {*} mthStr yyyy-MM
 * @returns  yyyy-MM-dd
 */
export function monthLastDayStr(mthStr){
  let monthTime = dayjs(mthStr, "YYYY-MM")
  let endDayTime =  monthTime.endOf('month')
  // 大于今天, 
  if(dayjs().isBefore(endDayTime)){
     return  formatDate(new Date())
  }
  return  endDayTime.format(DAY_FORMAT)
}

export function timeDays(beginDay,endDay){
  const start = new Date(beginDay);
  const end = new Date(endDay);
  const timeDiff = Math.abs(end - start);
  const days = Math.ceil(timeDiff / (1000 * 60 * 60 * 24)) + 1 
  return days
}


// 8点前, 放回前天, 8点 后返回昨天
export function lastDataDayTime(){
  const hour = dayjs().hour()
  if(hour<8){
    return  dayFormatDay(-2)
  }
  return  dayFormatDay(-1)
}

